// ignore_for_file: prefer_const_constructors, must_be_immutable
import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get_utils/src/extensions/internacionalization.dart';
import 'package:yt_devops/pages/alarm/alarm_home/alarm_home_model.dart';
import 'package:yt_devops/components/my_gesture_detector.dart';
import 'package:yt_devops/resources/str_res_keys.dart';
import 'package:yt_devops/styles/styles.dart';
import 'package:yt_devops/utils/enums.dart';

class AlarmChartWidget extends StatefulWidget {
  /// 紧急
  int? errorNum;

  /// 重要
  int? alarmNum;

  /// 次要
  int? warnNum;

  /// 提示
  int? infoNum;

  /// 告警总数
  int? alarmSum;
  List<int>? alarmNumList;
  final Function(int) callBack;
  AlarmChartWidget(
      {super.key,
      required this.errorNum,
      required this.alarmNum,
      required this.infoNum,
      required this.warnNum,
      required this.alarmSum,
      required this.alarmNumList,
      required this.callBack});

  @override
  State<AlarmChartWidget> createState() => _AlarmChartState();
}

class _AlarmChartState extends State<AlarmChartWidget> {
  AlarmNumState alarmNumState = AlarmNumState();
  BrnDoughnutDataItem? selectedItem;

  int count = 4;
  num alarmSum = 0;
  List<BrnDoughnutDataItem> dataList = [];

  List<String> alarmStatusList = [
    AlarmStatusGradeType.error.label,
    AlarmStatusGradeType.alarm.label,
    AlarmStatusGradeType.warn.label,
    AlarmStatusGradeType.info.label,
  ];

  List<Color> preinstallColors = [
    Color(0xFFFA3F3F),
    Color(0xFFF79631),
    Color(0xFFFAC958),
    Color(0xFF0F6EFF),
  ];
  int selectedIndex = 4;
  @override
  void initState() {
    super.initState();

    final Set<int> temp = {};
    temp.addAll(widget.alarmNumList!);

    if (temp.length == 1 && temp.first == 0) {
      dataList.add(
        BrnDoughnutDataItem(
          title: "",
          value: 100,
          color: Colour.chartBackgroundColor,
        ),
      );
    } else {
      for (int i = 0; i < count; i++) {
        dataList.add(
          BrnDoughnutDataItem(
            title: alarmStatusList[i],
            value: widget.alarmNumList![i].toDouble(),
            color: getColorWithIndex(i),
          ),
        );
      }
    }

    setState(() {});
  }

  @override
  void didUpdateWidget(covariant AlarmChartWidget oldWidget) {
    super.didUpdateWidget(oldWidget);

    if (oldWidget.alarmSum != widget.alarmSum) {
      dataList = [];
      for (int i = 0; i < count; i++) {
        dataList.add(
          BrnDoughnutDataItem(
            title: alarmStatusList[i],
            value: widget.alarmNumList![i].toDouble(),
            color: getColorWithIndex(i),
          ),
        );
      }
    }
  }

  Color getColorWithIndex(int index) {
    return preinstallColors[index % preinstallColors.length];
  }

  Widget generateLegend() {
    return ListView.builder(
      padding: EdgeInsets.zero,
      physics: const NeverScrollableScrollPhysics(),
      itemBuilder: (context, index) {
        return MyGestureDetector(
            onTap: () {
              setState(() {
                selectedIndex = index;
                widget.callBack(selectedIndex);
              });
            },
            child: Container(
              alignment: Alignment.centerLeft,
              color: Colors.white,
              margin: EdgeInsets.only(bottom: 10),
              child: Row(
                children: [
                  Container(
                    margin: EdgeInsets.only(right: 6.w),
                    decoration: BoxDecoration(
                      color: preinstallColors[index],
                      borderRadius: BorderRadius.all(
                        Radius.circular(2.sp),
                      ),
                    ),
                    width: 12.w,
                    height: 3,
                  ),
                  SizedBox(
                    height: 25,
                    child: Text(
                      alarmStatusList[index],
                      style: TextStyle(
                        color: selectedIndex == index
                            ? Colour.primaryColor
                            : Colour.mainTextColor,
                        fontSize: 12.sp,
                      ),
                    ),
                  ),
                ],
              ),
            ));
      },
      itemCount: alarmStatusList.length,
    );
  }

  Widget generateRandNum() {
    return ListView.builder(
      padding: EdgeInsets.zero,
      physics: const NeverScrollableScrollPhysics(),
      itemBuilder: (context, index) {
        return MyGestureDetector(
            onTap: () {
              setState(() {
                selectedIndex = index;
                widget.callBack(selectedIndex);
              });
            },
            child: Container(
              color: Colors.white,
              margin: EdgeInsets.only(bottom: 10),
              child: SizedBox(
                height: 25,
                child: Text(
                  widget.alarmNumList![index].toString(),
                  textAlign: TextAlign.end,
                  style: TextStyle(
                    color: selectedIndex == index
                        ? Colour.primaryColor
                        : Colour.mainTextColor,
                    fontSize: 16.sp,
                    fontWeight: FontWeight.w500,
                    fontFamily: 'DIN',
                  ),
                ),
              ),
            ));
      },
      itemCount: alarmStatusList.length,
    );
  }

  @override
  Widget build(BuildContext context) {
    alarmStatusList = [
      AlarmStatusGradeType.error.label,
      AlarmStatusGradeType.alarm.label,
      AlarmStatusGradeType.warn.label,
      AlarmStatusGradeType.info.label,
    ];
    return Container(
      height: 160,
      color: Colour.white,
      alignment: Alignment.centerLeft,
      child: Row(
        children: <Widget>[
          MyGestureDetector(
              onTap: () {
                setState(() {
                  selectedIndex = 4;
                  widget.callBack(selectedIndex);
                });
              },
              child: Stack(
                children: <Widget>[
                  Container(
                      color: Colors.white,
                      margin: EdgeInsets.only(
                        left: 32.w,
                        top: 16,
                        bottom: 16,
                      ),
                      child: BrnDoughnutChart(
                        ringWidth: 20,
                        width: 128.w,
                        height: 128.w,
                        data: dataList,
                        showTitleWhenSelected: true,
                      )),
                  Positioned(
                    left: 64.w,
                    top: 60,
                    child: Container(
                      alignment: Alignment.center,
                      // color: gr,
                      width: 65.w,
                      child: Column(
                        children: [
                          Text(
                            widget.alarmSum.toString(),
                            textAlign: TextAlign.center,
                            style: TextStyle(
                                fontSize: 18.sp,
                                fontWeight: FontWeight.bold,
                                fontFamily: 'DIN',
                                color: selectedIndex == 4
                                    ? Colour.primaryColor
                                    : Colour.mainTextColor),
                          ),
                          Text(
                            ResKey.alarmSum.tr,
                            textAlign: TextAlign.center,
                            style: TextStyle(
                              color: selectedIndex == 4
                                  ? Colour.primaryColor
                                  : Colour.lightTextColor,
                              fontWeight: FontWeight.w500,
                              fontSize: 12.sp,
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),
                ],
              )),
          Expanded(
            child: Container(
              color: Colors.white,
              margin: EdgeInsets.only(left: 25.w, top: 15),
              child: generateLegend(),
            ),
          ),
          Expanded(
            child: Container(
              // color: Colors.red,
              margin: EdgeInsets.only(right: 40.w, top: 15),
              child: generateRandNum(),
            ),
          ),
        ],
      ),
    );
  }
}
